import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
  constructor(
    private readonly usersService: UsersService,
    private readonly jwtService: JwtService
  ) { }

  async validateUser(username: string, pass: string): Promise<any> {
    // console.log(username)
    const user_object = await this.usersService.findOne(username, pass);
    if (user_object.status == 20000) {
      //   const { password, ...result } = user;
      return user_object.user_data;
    }
    return null;
  }

  async login(user: any) {
    const payload = { username: user.username, uid: user.userId, level: user.level };
    return {
      access_token: this.jwtService.sign(payload),
    };
  }

  async extend(user: any) {
    const payload = { username: user.username, uid: user.userId, level: user.level };
    return {
      access_token: this.jwtService.sign(payload),
    };
  }
}
